package edu.umn.msse.farside.FourH.web;

import edu.umn.msse.farside.FourH.domain.Event;
import edu.umn.msse.farside.FourH.domain.Position;
import edu.umn.msse.farside.FourH.domain.Volunteer;
import edu.umn.msse.farside.FourH.exceptions.DuplicateVolunteerException;
import edu.umn.msse.farside.FourH.services.EventService;
import edu.umn.msse.farside.FourH.services.VolunteerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import java.sql.Timestamp;

/**
 * Created by IntelliJ IDEA.
 * User: corcutt
 * Date: Mar 18, 2009
 * Time: 10:22:32 AM
 * To change this template use File | Settings | File Templates.
 */
//@RunWith(value = SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations={"/applicationContext.xml"})
@Controller
@RequestMapping("/loadTestData")
public class LoadTestData
{
    private static final String EMAIL3 = "user3@domain.com";
    private static final String PASSWORD3 = "password3";
    private static final String FNAME3 = "John";
    private static final String LNAME3 = "Smith";

    private static final String EMAIL1 = "bugsbunny@warnerbrother.com";
    private static final String PASSWORD1 = "bb";
    private static final String FNAME1 = "Bugs";
    private static final String LNAME1 = "Bunny";

    private static final String EMAIL2 = "elmerfudd@warnerbrother.com";
    private static final String PASSWORD2 = "ef";
    private static final String FNAME2 = "Elmer";
    private static final String LNAME2 = "Fudd";

    private static final String EMAIL = "caorcutt@gmail.com";
    private static final String PASSWORD = "cao";
    private static final String FNAME = "Cheryl";
    private static final String LNAME = "Orcutt";

    @Autowired(required = true)
    EventService eventService;

    @Autowired(required = true)
    VolunteerService volunteerService;

    @Autowired(required = true)
    EntityManagerFactory entityManagerFactory;

    @RequestMapping(method = RequestMethod.GET)
    public String doIt() {
        // make sure the db is empty first
        //clearAllData();
        // create the volunteers
        createVolunteers();
        Volunteer volunteer = volunteerService.findVolunteer("caorcutt@gmail.com");
        // create events
        Event evt1 = createEvent("First Event", "Event1 description", volunteer);
        createEvent1WithPositions();
        createEvent2WithPositions();

        Event evt2 = createEvent("Second Event", "Event2 description", volunteer);
        Event evt3 = createEvent("third Event", "Event3 description", volunteer);
        eventService.addEvent(evt1);
        eventService.addEvent(evt2);
        eventService.addEvent(evt3);

        return("redirect:futureEvents");

    }

    public void createVolunteers()
    {
        try
        {
            volunteerService.addVolunteer(new Volunteer(EMAIL1, PASSWORD1, FNAME1, LNAME1));
        }
        catch (DuplicateVolunteerException e)
        {
            // do not worry about DuplicateVolunteerException
        }
        try
        {
            volunteerService.addVolunteer(new Volunteer(EMAIL2, PASSWORD2, FNAME2, LNAME2));
        }
        catch (DuplicateVolunteerException e)
        {
            // do not worry about DuplicateVolunteerException
        }
        try
        {
            volunteerService.addVolunteer(new Volunteer(EMAIL, PASSWORD, FNAME, LNAME));
        }
        catch (DuplicateVolunteerException e)
        {
            // do not worry about DuplicateVolunteerException
        }
        try
        {
            volunteerService.addVolunteer(new Volunteer(EMAIL3, PASSWORD3, FNAME3, LNAME3));
        }
        catch (DuplicateVolunteerException e)
        {
            // do not worry about DuplicateVolunteerException
        }
    }

    /* private methods to aid in test setup */
    private Event createEvent1WithPositions()
    {
        Event evt1 = createEvent1();
        eventService.addEvent(evt1);
        Position ePos = new Position("Position1", "This is the admin position", 1);
        Position ePos2 = new Position("Server", "This is the kitchen position", 4);
        Position ePos3 = new Position("Cashier", "This is the money position", 1);

        // add positions to the event
        evt1.addPosition(ePos);

        evt1.addPosition(ePos2);

        evt1.addPosition(ePos3);

        evt1 = eventService.updateEvent(evt1);
        return evt1;
    }

    private Event createEvent1()
    {
        java.util.Date today = new java.util.Date();
        Timestamp startCal = new java.sql.Timestamp(today.getTime());
        Timestamp endCal = new java.sql.Timestamp(today.getTime());
        startCal.setTime(today.getTime()+(60*60*60*24*60));  // 4 days
        endCal.setTime(today.getTime()+(60*60*60*24*90));   // 7 days
        Volunteer admin = volunteerService.findVolunteer("caorcutt@gmail.com");
        Event evt1 = new Event("Event1", "Event1 description", admin, startCal, endCal);
        return evt1;
    }

    private Event createEvent2WithPositions()
    {
        Event evt1 = createEvent2();
        eventService.addEvent(evt1);
        Position ePos = new Position("Security", "This is security position", 18);
        Position ePos2 = new Position("Server", "This is the kitchen position", 2);
        Position ePos3 = new Position("Cashier", "This is the money position", 1);
        Position ePos4 = new Position("Manager", "Who wants a manager", 2);

        // add positions to the event
        evt1.addPosition(ePos);

        evt1.addPosition(ePos2);

        evt1.addPosition(ePos3);

        evt1.addPosition(ePos4);

        evt1 = eventService.updateEvent(evt1);
        return evt1;
    }

    private Event createEvent2()
    {
        java.util.Date today = new java.util.Date();
        Timestamp startCal = new java.sql.Timestamp(today.getTime());
        Timestamp endCal = new java.sql.Timestamp(today.getTime());
        startCal.setTime(today.getTime()+(60*60*60*24*60));  // 4 days
        endCal.setTime(today.getTime()+(60*60*60*24*90));   // 7 days
        Volunteer admin = volunteerService.findVolunteer("bugsbunny@warnerbrother.com");
        Event evt1 = new Event("State fair", "Minnesota state fair", admin, startCal, endCal);
        return evt1;
    }

    private Event createEvent(String name, String description, Volunteer user)
    {
        Timestamp startCal = java.sql.Timestamp.valueOf("2009-06-20 10:00:00");
        Timestamp endCal = java.sql.Timestamp.valueOf("2009-06-28 20:00:00");
        Event evt1 = new Event(name, description, user, startCal, endCal);
        return evt1;
    }

    private void clearAllData() {
        EntityManager em = entityManagerFactory.createEntityManager();
        EntityTransaction tx = em.getTransaction();

        tx.begin();
        for (Object i : em.createQuery("from Event").getResultList()) {
            em.remove(i);
        }

        for (Object i : em.createQuery("from Volunteer").getResultList()) {
            em.remove(i);
        }
        tx.commit();
    }
}
